package com.shadow.baiqun.delegate.receive;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.shadow.baiqun.R;
import com.shadow.baiqun.R2;
import com.shadow.baiqun.delegate.receive.adapter.FragmentAdapter;
import com.shadow.baiqun.delegate.receive.locationcheck.LocationCheckConverter;
import com.shadow.baiqun.delegate.receive.pregoods.GoodsListDelegate;
import com.shadow.baiqun.delegate.receive.waitput.WaitPutDelegate;
import com.shadow.baiqun.global.GlobalDataManager;
import com.shadow.core.delegate.ShadowDelegate;
import com.shadow.core.delegate.bottom.BottomItemDelegate;
import com.shadow.core.net.callback.IError;
import com.shadow.core.net.callback.ISuccess;
import com.shadow.core.net.rx.RxRestClient;
import com.shadow.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * @author zy
 * on 2018/7/8
 */
public class ReceiveDelegate extends BottomItemDelegate {
    @BindView(R2.id.tab)
    TabLayout mTab;
    @BindView(R2.id.viewPager)
    ViewPager mViewPager;
    private Disposable mDisposable;

    @Override
    public Object setLayout() {
        return R.layout.delegate_receive;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTab.addTab(mTab.newTab().setText("预收货列表"));
        mTab.addTab(mTab.newTab().setText("待入库列表"));
        List<ShadowDelegate> list = new ArrayList<>();
        list.add(GoodsListDelegate.create());
        list.add(WaitPutDelegate.create());
        mViewPager.setAdapter(new FragmentAdapter(getChildFragmentManager(), list));
        mTab.setupWithViewPager(mViewPager);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        requestLocationData();
    }

    /**
     * 请求库位列表
     */
    private void requestLocationData() {
        if (GlobalDataManager.getInstance().getLocationEntityList() == null) {
            mDisposable = RxRestClient.builder()
                    .url("WMS/backstage/collectgoods/getDepotsList")
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            LocationCheckConverter dataConverter = new LocationCheckConverter();
                            dataConverter.setJsonData(response);
                            List<MultipleItemEntity> list = dataConverter.convertLocation();
                            if (list != null && !list.isEmpty()) {
                                GlobalDataManager.getInstance().setLocationEntityList(list);
                            }
                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String msg) {

                        }
                    })
                    .build()
                    .unRestGet();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(mDisposable != null){
            mDisposable.dispose();
        }
    }
}
